<?php
/////////////////////////////////////////////////////////////////////////////
// 这个文件是 网城创想多用户商城 项目的一部分
//
// Copyright (c) 2007 - 2008 www.shopnc.net 
//
// 要查看完整的版权信息和许可信息，请查看源代码中附带的 COPYRIGHT 文件，
// 或者访问 http://www.shopnc.net/ 获得详细信息。
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : sqlback.manage.php   FILE_PATH : E:\web\mulitishop\system\sqlback.manage.php
 * 数据库备份
 *
 * @copyright Copyright (c) 2007 - 2007 www.shopnc.net 
 * @author 
 * @package 
 * @subpackage 
 * @version Fri Dec 07 15:19:39 CST 2007
 */

require_once("../global.inc.php");

class SqlbackManage extends SystemFrameWork{
	/**
	 * 数据库备份对象
	 * 
	 * @var obj
	 */
	var $obj_sql_back;
	/**
	 * 验证对象
	 *
	 * @var obj
	 */
	var $obj_validate;
	
	function main(){
		/**
		 * 创建加价操作对象
		 */
		if (!is_object($this->obj_sql_back)){
			require_once("system_sqlback.class.php");
			$this->obj_sql_back = new SqlBackClass();
		}
		/**
		 * 创建验证对象
		 */
		if (!is_object($this->obj_validate)){
			require_once("commonvalidate.class.php");
			$this->obj_validate = new CommonValidate();
		}		
		
		/**
		 * 设置模板路径
		 */
		$this->setsubtemplates("system");
		
		/**
		 * 语言包
		 */
		$this->getlang("system_sqlback");

		switch ($this->_input['action']){
			case "list":
				$this->_list();
				break;
			case "backup":
				$this->_backup();
				break;
			case "optimize":
				$this->_optimize();
				break;
			case "info":
				$this->_info();
				break;
			case "restore":
				$this->_restore();
				break;
			case "restore_two":
				$this->_restore_two();
				break;
			case "del":
				$this->_del();
				break;
			default:
				$this->_list();
				break;
		}
	}
	
	/**
	 * 数据表表名列表
	 */
	function _list(){
		/**
		 * 取数据结构
		 */
		$sql_array = $this->obj_sql_back->sqlList();
		
		/**
		 * 页面输出
		 */
		$this->output('sql_array',$sql_array);
		$this->showpage('sys_sql_back.manage');
	}
	
	/**
	 * 数据库备份操作
	 */
	function _backup(){
		$this->obj_validate->validateparam = array(
		array("input"=>$this->_input["sql_name"],"require"=>"true","message"=>'请选择需要备份的数据表'),
		array("input"=>$this->_input["vol_size"],"require"=>"true","validator"=>"Number","message"=>'文件长度为空或不为数字'),
		);/*数据表表名*/
		$error = $this->obj_validate->validate();

		if($error != ""){
			$this->redirectPath('succ',"system/sqlback.manage.php?action=list",$error);
			exit;
		}
		
		/**
		 * 数据库备份
		 */
		$this->obj_sql_back->dumpSql($this->_input["sql_name"],$this->_input["vol_size"],$this->_input["bak_file_name"]);
		$url = "system/sqlback.manage.php?action=list";
		$this->redirectPath('succ',$url,"数据库备份完成");		
	}
	
	/**
	 * 优化表
	 */
	function _optimize(){
		/**
		 * 取数据结构
		 */
		$sql_array = $this->obj_sql_back->sqlList();
		if (is_array($sql_array)){
			foreach ($sql_array as $k => $v){
				$line .= "`".$v['Name']."`,";
			}
			$line = trim($line,',');
		}
		/**
		 * 优化表
		 */
		$this->obj_sql_back->optimizeTable($line);
		$url = "system/sqlback.manage.php?action=list";
		$this->redirectPath('succ',$url,"数据库优化完成");
	}
	
	/**
	 * 取表结构
	 */
	function _info(){
		$this->obj_validate->validateparam = array(
		array("input"=>$this->_input["name"],"require"=>"true","message"=>'表名为空'),
		);/*数据表表名*/
		$error = $this->obj_validate->validate();

		if($error != ""){
			$this->redirectPath('succ',"system/sqlback.manage.php?action=list",$error);
			exit;
		}
		/**
		 * 取表结构
		 */
		$sql = $this->obj_sql_back->infoTable($this->_input["name"]);
		/**
		 * 页面输出
		 */
		$this->output('sql',$sql);
		$this->showpage('sys_sql_back.info');
	}
	
	/**
	 * 还原表
	 */
	function _restore(){
		/*取间隔符*/
		$b = $this->obj_sql_back->getIntervals();
		if ($handle = @opendir('..'.$b.'sqlback')) {
			$i = 0;
		    while (false !== ($file = @readdir($handle))) {
		        if ($file != "." && $file != ".." && strstr($file,'.sql')) {
		        	$file_line = @substr($file,0,-4);
		        	$vol_array[$i] = @explode("_",$file_line);
		        	$pathfile = '..'.$b.'sqlback'.$b.$file;
		            $file_array[$i]['name'] = $file;
		            $file_array[$i]['size'] = intval((@filesize($pathfile)/1000)).'k';
		           	$file_array[$i]['time'] = @date("Y-m-d H:i:s",@filemtime($pathfile));
		           	$i++;
		        }
		    }
		    @closedir($handle);
		}
		/**
		 * 页面输出
		 */
		$this->output('vol_array',$vol_array);
		$this->output('file_array',$file_array);
		$this->showpage('sys_sql_back.restore');
	}
	
	/**
	 * 还原数据库
	 */
	function _restore_two(){
		$this->obj_validate->validateparam = array(
		array("input"=>$this->_input["filename"],"require"=>"true","message"=>'文件名为空'),
		);/*数据表表名*/
		$error = $this->obj_validate->validate();

		if($error != ""){
			$this->redirectPath('succ',"system/sqlback.manage.php?action=restore",$error);
			exit;
		}

		$file_line = @substr($this->_input["filename"],0,-4);
		$file_line_array = @explode("_",$file_line);
		/*取SQL文件*/
		$b = $this->obj_sql_back->getIntervals();
		if ($handle = @opendir('..'.$b.'sqlback')) {
		    while (false !== ($file = @readdir($handle))) {
		        if ($file != "." && $file != "..") {
		        	$file_line = @substr($file,0,-4);
		        	$vol_array = @explode("_",$file_line);
		        	/*判断是否有分卷文件*/
		        	if ($file_line_array[0].$file_line_array[1].$file_line_array[2] == $vol_array[0].$vol_array[1].$vol_array[2]){
		        		$restore_file[] = $file;
		        	}
		        }
		    }
		    @closedir($handle);
		}
		
		/*还原数据库*/
		$flag = $this->obj_sql_back->do_safe_restore($restore_file);
		$url = "system/sqlback.manage.php?action=list";
		$this->redirectPath('succ',$url,"数据库还原完成");
	}
	
	/**
	 * 删除文件
	 */
	function _del(){
		$this->obj_validate->validateparam = array(
		array("input"=>$this->_input["filename"],"require"=>"true","message"=>'文件名为空'),
		);/*数据表表名*/
		$error = $this->obj_validate->validate();

		if($error != ""){
			$this->redirectPath('succ',"system/sqlback.manage.php?action=restore",$error);
			exit;
		}
		
		
		$file_line = @substr($this->_input["filename"],0,-4);
		$file_line_array = @explode("_",$file_line);
		/*取SQL文件*/
		$b = $this->obj_sql_back->getIntervals();
		if ($handle = @opendir('..'.$b.'sqlback')) {
		    while (false !== ($file = @readdir($handle))) {
		        if ($file != "." && $file != "..") {
		        	$file_line = @substr($file,0,-4);
		        	$vol_array = @explode("_",$file_line);
		        	/*判断是否有分卷文件*/
		        	if ($file_line_array[0].$file_line_array[1].$file_line_array[2] == $vol_array[0].$vol_array[1].$vol_array[2]){
		        		$restore_file[] = $file;
		        	}
		        }
		    }
		    @closedir($handle);
		}
		
		if (is_array($restore_file)){
			foreach ($restore_file as $v){
				@unlink ('..'.$b.'sqlback'.$b.$v);
			}
		}
		$this->redirectPath('succ',"system/sqlback.manage.php?action=restore","删除成功");
	}
	
	
}

$sqlbac_manage = new SqlbackManage();
$sqlbac_manage->main();
unset($sqlbac_manage);
?>